/**
 * Created by zhuhuijun on 2016/4/20
 * 系统日志服务实现
 *   logger.log('这是logger的log日志');-->这是logger的log日志
 *   logger.warn('这是logger的warn日志');-->这是logger的warn日志
 *   logger.error('这是logger的{0}日志',['error']);-->这是logger的error日志
 *   logger.debug('这是logger的{0}日志}', ['debug' ]);-->这是logger的debug日志}
 */
angular.module('app.libs', []);
angular.module('app.libs').provider('Logger', [function () {
    var isEnabled = true;
    this.enabled = function (_isEnabled) {
        isEnabled = !!_isEnabled;
    }
    this.$get = ['$log', function ($log) {
        var Logger = function (context) {
            this.context = context;
        }

        Logger.getInstance = function (context) {
            return new Logger(context);
        }
        //替换
        Logger.supplant = function (str, o) {
            return str.replace(/\{([^{}]*)\}/g, function (a, b) {
                var r = o[b];
                if (angular.isObject(r)) {
                    r = angular.toJson(r);
                }
                return typeof r === 'string' || typeof r === 'number' ? r : a;
            })
        }
        //格式化时间
        Logger.getFormattedTimestamp = function (date) {
            return Logger.supplant('{0}:{1}:{2}:{3}', [
                date.getHours(),
                date.getMinutes(),
                date.getSeconds(),
                date.getMilliseconds()
            ]);
        }
        Logger.prototype = {
            _log: function (originalFn, args) {
                if (!isEnabled)return;
                var now = Logger.getFormattedTimestamp(new Date());
                var message = '', supplantData = [];
                switch (args.length) {
                    case 1:
                        message = Logger.supplant("{0} - {1}:{2}", [now, this.context, args[0]]);
                        break;
                    case 3:
                        supplantData = args[2];
                        message = Logger.supplant("{0} - {1}::{1}(\'{3}\')", [now, this.context, args[0], args[1]]);
                        break;
                    case 2:
                        if (typeof args[1] === 'string') {
                            message = Logger.supplant("{0} - {1}::{2}(\'{3}\')", [now, this.context, args[0], args[1]]);
                        } else {
                            if (angular.isArray(args[1])) {
                                supplantData = args[1];
                                message = Logger.supplant("{0} - {1}:{2}", [now, this.context, args[0], args[1]]);
                            }
                        }
                        break;
                }
                $log[originalFn].call(null, Logger.supplant(message, supplantData));
            },
            log: function () {
                this._log('log', arguments);
            },
            info: function () {
                this._log('info', arguments);
            },
            warn: function () {
                this._log('warn', arguments);
            },
            debug: function () {
                this._log('debug', arguments);
            },
            error: function () {
                this._log('error', arguments);
            }
        }
        return Logger;
    }]
}]);